/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package app.gnucash.client.widget;

import app.gnucash.client.AppEvents;
import app.gnucash.client.MainEntryPoint;
import app.gnucash.client.thesisServiceAsync;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.EventType;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 *
 * @author cc
 */
public class LoginDialog extends Dialog {

    protected TextField<String> userName;
    protected TextField<String> password;
    protected Button reset;
    protected Button login;
    protected Status status;

    public LoginDialog() {
        FormLayout layout = new FormLayout();
        layout.setLabelWidth(90);
        layout.setDefaultWidth(155);
        setLayout(layout);

        setButtonAlign(HorizontalAlignment.LEFT);
        setButtons("");
        setIcon(IconHelper.createStyle("user"));
        setHeading("GnuCash系统登录");
        setModal(true);
        setBodyBorder(true);
        setBodyStyle("padding: 8px;background: none");
        setWidth(300);
        setResizable(false);

        KeyListener keyListener = new KeyListener() {

            @Override
            public void componentKeyUp(ComponentEvent event) {
                validate();
            }
        };

        userName = new TextField<String>();
        userName.setMinLength(2);
        userName.setFieldLabel("用户名");
        userName.addKeyListener(keyListener);
        add(userName);

        password = new TextField<String>();
        password.setMinLength(4);
        password.setPassword(true);
        password.setFieldLabel("密码");
        password.addKeyListener(keyListener);
        add(password);

        setFocusWidget(userName);

    }

    @Override
    protected void createButtons() {
        super.createButtons();
        status = new Status();
        status.setBusy("请稍候...");
        status.hide();
        status.setAutoWidth(true);
        getButtonBar().add(status);

        getButtonBar().add(new FillToolItem());

        reset = new Button("重置");
        reset.setHeight(30);
        reset.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                userName.reset();
                password.reset();
                validate();
                userName.focus();
            }
        });

        login = new Button("登录");
        login.setHeight(30);
        login.disable();
        login.addSelectionListener(new SelectionListener<ButtonEvent>() {

            public void componentSelected(ButtonEvent ce) {
                onSubmit();
            }
        });

        addButton(reset);
        addButton(login);
    }

    protected void onSubmit() {
        status.show();
        getButtonBar().disable();
        String strUser = userName.getValue();
        String strPwd = password.getValue();
        thesisServiceAsync service = (thesisServiceAsync) Registry.get(MainEntryPoint.SERVICE);
        service.login(strUser, strPwd, new AsyncCallback<String>() {

            public void onFailure(Throwable caught) {
                Dispatcher.forwardEvent((EventType) Registry.get("AppInitError"), caught);
            }

            public void onSuccess(String result) {
                if (!result.equals("")) {
                    LoginDialog.this.hide();
                    AppEvent env = new AppEvent(AppEvents.Init);
                    env.setData("user", result);
                    Dispatcher.forwardEvent(env);
                } else {
                    userName.reset();
                    password.reset();
                    getButtonBar().enable();
                    login.disable();
                    status.setStyleName("error");
                    status.clearStatus("登录失败！");
                    //status.setText("Login Failed");
                    status.show();

                }
                //Dispatcher.forwardEvent((EventType) Registry.get("AppInitSuccess"), result);
            }
        });
//        Timer t = new Timer() {
//
//            @Override
//            public void run() {
//                LoginDialog.this.hide();
//            }
//        };
//        t.schedule(2000);
    }

    protected boolean hasValue(TextField<String> field) {
        return field.getValue() != null && field.getValue().length() > 0;
    }

    protected void validate() {
        login.setEnabled(hasValue(userName) && hasValue(password)
                && password.getValue().length() > 3);
    }
}
